package Controller.Actions.Search;

import Controller.*;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import Model.Beans.Results.*;
import Model.Beans.*;
import Model.QBE.*;

public class SearchNotRegisteredCoursesAction implements Action {
    
    private String name;
    
    public SearchNotRegisteredCoursesAction() 
    {
        this.name = "SearchNotRegisteredCourses";
    }
    
    public String getName()
    {
        return name;
    }
    
    public void perform(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException
    {
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");
        String id = request.getParameter("student_id");
        
        //Modelo de schedule
        ScheduleModel smodel = new ScheduleModel();
        //Modelo de course
        StudentModel stmodel = new StudentModel();
        CourseModel cmodel = new CourseModel();
        //List donde guardaremos id student, id curso
        ScheduleList lists = new ScheduleList();
        //Objeto donde almacenaremos el resultado
        Student_Course registeredCourses = new Student_Course();
        registeredCourses.setStudent(stmodel.search_by_id(id));

        
        //objecto temporal para hacer la busqueda
        Schedule tmp = new Schedule();
        tmp.setStudentId(Integer.parseInt(id));
        
        //Buscamos los ids de cursos
        lists = smodel.search(tmp);
        
        //Ahora iteramos la lista para obtener los datos de cada curso
        ArrayList courses = lists.getSchedule();
        
        for(int i=0; i<courses.size(); i++)
        {
            int course_id = ((Schedule)courses.get(i)).getCourseId();
            registeredCourses.addCourse(cmodel.search_by_id(Integer.toString(course_id)));
        }
        
        //Todos los cursos
        CourseList allCourses = cmodel.search(new Course());        
        boolean flag;
        
        for(int i=0; i<registeredCourses.getCourses().size(); i++)
        {
            Course scourse = (Course)(registeredCourses.getCourses().get(i));
            allCourses.delCourse(scourse);
            
        }
        
        response.getWriter().write(allCourses.toXML());
    }
    
}
